package com.jy.categories.service.impl;

import java.util.List;

import com.jy.books.domain.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.jy.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.jy.categories.mapper.CategoriesMapper;
import com.jy.categories.domain.Categories;
import com.jy.categories.service.ICategoriesService;

/**
 * 图书分类信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class CategoriesServiceImpl implements ICategoriesService 
{
    @Autowired
    private CategoriesMapper categoriesMapper;

    /**
     * 查询图书分类信息
     * 
     * @param categoryId 图书分类信息主键
     * @return 图书分类信息
     */
    @Override
    public Categories selectCategoriesByCategoryId(Long categoryId)
    {
        return categoriesMapper.selectCategoriesByCategoryId(categoryId);
    }

    /**
     * 查询图书分类信息列表
     * 
     * @param categories 图书分类信息
     * @return 图书分类信息
     */
    @Override
    public List<Categories> selectCategoriesList(Categories categories)
    {
        return categoriesMapper.selectCategoriesList(categories);
    }

    /**
     * 新增图书分类信息
     * 
     * @param categories 图书分类信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCategories(Categories categories)
    {
        int rows = categoriesMapper.insertCategories(categories);
        insertBooks(categories);
        return rows;
    }

    /**
     * 修改图书分类信息
     * 
     * @param categories 图书分类信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCategories(Categories categories)
    {
        categoriesMapper.deleteBooksByCategoryId(categories.getCategoryId());
        insertBooks(categories);
        return categoriesMapper.updateCategories(categories);
    }

    /**
     * 批量删除图书分类信息
     * 
     * @param categoryIds 需要删除的图书分类信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCategoriesByCategoryIds(Long[] categoryIds)
    {
        categoriesMapper.deleteBooksByCategoryIds(categoryIds);
        return categoriesMapper.deleteCategoriesByCategoryIds(categoryIds);
    }

    /**
     * 删除图书分类信息信息
     * 
     * @param categoryId 图书分类信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCategoriesByCategoryId(Long categoryId)
    {
        categoriesMapper.deleteBooksByCategoryId(categoryId);
        return categoriesMapper.deleteCategoriesByCategoryId(categoryId);
    }

    /**
     * 新增存储图书基本信息信息
     * 
     * @param categories 图书分类信息对象
     */
    public void insertBooks(Categories categories)
    {
        List<Books> booksList = categories.getBooksList();
        Long categoryId = categories.getCategoryId();
        if (StringUtils.isNotNull(booksList))
        {
            List<Books> list = new ArrayList<Books>();
            for (Books books : booksList)
            {
                books.setCategoryId(categoryId);
                list.add(books);
            }
            if (list.size() > 0)
            {
                categoriesMapper.batchBooks(list);
            }
        }
    }
}
